package com.zenitera.bigdata.sink;

import com.alibaba.fastjson.JSON;
import com.zenitera.bigdata.bean.WaterSensor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;
import org.apache.flink.streaming.connectors.kafka.KafkaSerializationSchema;
import org.apache.kafka.clients.producer.ProducerRecord;

import javax.annotation.Nullable;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Properties;

public class Flink01_Sink_Kafka02 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);

        ArrayList<WaterSensor> waterSensors = new ArrayList<>();
        waterSensors.add(new WaterSensor("sensor_1", 1607527992000L, 20));
        waterSensors.add(new WaterSensor("sensor_1", 1607527994000L, 30));
        waterSensors.add(new WaterSensor("sensor_1", 1607527996000L, 40));
        waterSensors.add(new WaterSensor("sensor_1", 1607527998000L, 50));
        waterSensors.add(new WaterSensor("sensor_2", 1607527993000L, 10));
        waterSensors.add(new WaterSensor("sensor_2", 1607527995000L, 30));
        waterSensors.add(new WaterSensor("sensor_2", 1607527997000L, 40));

        DataStreamSource<WaterSensor> stream = env.fromCollection(waterSensors);
        Properties sinkConfig = new Properties();
        sinkConfig.setProperty("bootstrap.servers", "wangtingflink01:9092,wangtingflink02:9092,wangtingflink03:9092");
        stream
                .keyBy(WaterSensor::getId)
                .sum("vc")
                .addSink(new FlinkKafkaProducer<WaterSensor>("default",
                        new KafkaSerializationSchema<WaterSensor>() {
                            @Override
                            public ProducerRecord<byte[], byte[]> serialize(WaterSensor element,
                                                                            @Nullable Long timestamp) {
                                String s = JSON.toJSONString(element);
                                return new ProducerRecord<>("Flink01_Sink_Kafka", s.getBytes(StandardCharsets.UTF_8));
                            }
                        },
                        sinkConfig,
                        FlinkKafkaProducer.Semantic.AT_LEAST_ONCE

                ));
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}


/*

[wangting@hadoop102 ~]$ kafka-console-consumer.sh --bootstrap-server hadoop102:9092 --topic Flink01_Sink_Kafka
...
{"id":"sensor_1","ts":1607527992000,"vc":20}
{"id":"sensor_1","ts":1607527992000,"vc":50}
{"id":"sensor_1","ts":1607527992000,"vc":90}
{"id":"sensor_1","ts":1607527992000,"vc":140}
{"id":"sensor_2","ts":1607527993000,"vc":10}
{"id":"sensor_2","ts":1607527993000,"vc":40}
{"id":"sensor_2","ts":1607527993000,"vc":80}
 */